package com.pocketsweet.chatlib.utils;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FunctionCallback;
import com.avos.avoscloud.GetCallback;
import com.pocketsweet.MLCache;
import com.pocketsweet.R;
import com.pocketsweet.MLApplication;
import com.pocketsweet.chatui.domain.User;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.utils.ToolKits;
import com.squareup.picasso.Picasso;

public class UserUtils {

	public static void setUserInfo(final Context context,
			final String username, final TextView nickview,
			final ImageView imageView, final TextView tvInvisibleAvatar,
			final TextView tvInvisibleNick) {
		JSONObject user = MLCache.getCache().getAsJSONObject(username);
		if (user != null) {
			try {
				Picasso.with(context).load(user.getString("subAvatar"))
						.placeholder(R.drawable.default_user_avatar)
						.transform(new PicassoSquare()).into(imageView);

				// UserService.displaySquare(user.getString("subAvatar"),
				// imageView);
				nickview.setText(user.getString("nickname"));
				tvInvisibleAvatar.setText(user.getString("subAvatar"));
				tvInvisibleNick.setText(user.getString("nickname"));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			AVQuery<MLUser> query = new AVQuery<MLUser>("_User");
			query.getInBackground(username, new GetCallback<MLUser>() {

				@Override
				public void done(MLUser user, AVException e) {
					if (e == null) {
						if (user.getSubAvatar() == null||user.getSubAvatar().equals("")) {// 兼容上一版本
							Map<String, Object> params = new HashMap<String, Object>();
							params.put("userId", username);
							Picasso.with(context)
									.load(user.getAvatarUrl())
									.placeholder(R.drawable.default_user_avatar)
									.transform(new PicassoSquare())
									.into(imageView);
							AVCloud.callFunctionInBackground("creatSubAvatar",
									params, new FunctionCallback<Object>() {

										@Override
										public void done(Object o, AVException e) {
											if (e == null) {

												AVQuery<MLUser> query = new AVQuery<MLUser>(
														"_User");
												query.getInBackground(
														username,
														new GetCallback<MLUser>() {

															@Override
															public void done(
																	MLUser newuser,
																	AVException e) {
																MLCache.saveStranger(newuser);

																// Picasso.with(
																// context)
																// .load(newuser
																// .getSubAvatar())
																// .placeholder(
																// R.drawable.default_user_avatar)
																// .transform(
																// new
																// PicassoSquare())
																// .into(imageView);
																tvInvisibleAvatar
																		.setText(newuser
																				.getSubAvatar());
																tvInvisibleNick
																		.setText(newuser
																				.getNickname());

															}
														});
											} else {
												ToolKits.toast(context,
														"发送邀请失败，请检查网络");
											}
										}

									});
						} else {
							Picasso.with(context)
									.load(user.getSubAvatar())
									.placeholder(R.drawable.default_user_avatar)
									.transform(new PicassoSquare())
									.into(imageView);
							// UserService.displaySquare(user.getSubAvatar(),
							// imageView);
							MLCache.saveStranger(user);
							tvInvisibleAvatar.setText(user.getSubAvatar());
							tvInvisibleNick.setText(user.getNickname());
						}
						nickview.setText(user.getNickname());
					}
				}
			});

		}
	}
}
